Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@datastructures-js/binary-search-tree

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@datastructures-js/binary-search-tree

binary search tree & avl tree (self balancing tree) implementation in javascript

  • 3.1.8
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@datastructures-js/binary-search-tree

build:? npm npm npm

Binary Search Tree & AVL Tree (Self Balancing Tree) implementation in javascript.

Binary Search Tree Binary Search Tree
AVL Tree
(Self Balancing Tree)
AVL Tree

Table of Contents

install

npm install --save @datastructures-js/binary-search-tree

API

Both trees have the same interface except that AVL tree will maintain itself balanced by rotating the nodes that become unbalanced during insertion and deletion. If your code requires a strictly balanced tree that always benefits from the log(n) runtime of insert & remove, you should use the AVL one.

require

const { BinarySearchTree, AvlTree } = require('@datastructures-js/binary-search-tree');

import

import { BinarySearchTree, AvlTree } from '@datastructures-js/binary-search-tree';

Construction

const bst = new BinarySearchTree();

// OR a self balancing tree

const bst = new AvlTree();

.insert(key, value)

inserts a node with key/value into the tree. Inserting an node with existing key, would update the existing node's value with the new one. AVL tree will rotate nodes properly if the tree becomes unbalanced during insertion.

params
nametype
keynumber or string
valueobject
return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode
runtime
O(log(n))
Example
bst.insert(50, 'v1');
bst.insert(80, 'v2');
bst.insert(30, 'v3');
bst.insert(90, 'v4');
bst.insert(60, 'v5');
bst.insert(40, 'v6');
bst.insert(20, 'v7');

.has(key)

checks if a node exists by its key.

params
nametype
keynumber or string
return
boolean
runtime
O(log(n))
Example
bst.has(50); // true
bst.has(100); // false

.find(key)

finds a node in the tree by its key.

params
nametype
keynumber or string
return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode
runtime
O(log(n))
Example
const n60 = bst.find(60);
console.log(n60.getKey()); // 60
console.log(n60.getValue()); // v5

console.log(bst.find(100)); // null

.min()

finds the node with min key in the tree.

return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode
runtime
O(log(n))
Example
const min = bst.min();
console.log(min.getKey()); // 20
console.log(min.getValue()); // v7

.max()

finds the node with max key in the tree.

return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode
runtime
O(log(n))
Example
const max = bst.max();
console.log(max.getKey()); // 90
console.log(max.getValue()); // v4

.root()

returns the root node of the tree.

return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode
runtime
O(1)
Example
const root = bst.root();
console.log(root.getKey()); // 50
console.log(root.getValue()); // v1

.count()

returns the count of nodes in the tree.

return
number
runtime
O(1)
Example
console.log(bst.count()); // 7

.traverseInOrder(cb)

traverses the tree in order (left-node-right).

params
nametypedescription
cbfunctioncalled with each node
runtime
O(n)
Example
bst.traverseInOrder((node) => console.log(node.getKey()));

/*
20
30
40
50
60
80
90
*/

.traversePreOrder(cb)

traverses the tree pre order (node-left-right).

params
nametypedescription
cbfunctioncalled with each node
runtime
O(n)
Example
bst.traversePreOrder((node) => console.log(node.getKey()));

/*
50
30
20
40
80
60
90
*/

.traversePostOrder(cb)

traverses the tree post order (left-right-node).

params
nametypedescription
cbfunctioncalled with each node
runtime
O(n)
Example
bst.traversePostOrder((node) => console.log(node.getKey()));

/*
20
40
30
60
90
80
50
*/

.remove(key)

removes a node from the tree by its key. AVL tree will rotate nodes properly if the tree becomes unbalanced during deletion.

params
nametype
keynumber or string
return
boolean
runtime
O(log(n))
Example
bst.remove(20); // true
bst.remove(100); // false
console.log(bst.count()); // 6

.clear()

clears the tree.

runtime
O(1)
Example
bst.clear();
console.log(bst.count()); // 0
console.log(bst.root()); // null

BinarySearchTreeNode

.getKey()

returns the node's key that is used to compare with other nodes.

return
number or string
.setValue(value)

change the value that is associated with a node.

params
nametype
valueobject
.getValue()

returns the value that is associated with a node.

return
object
.getLeft()

returns node's left child node.

return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode
.getRight()

returns node's right child node.

return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode
.getParent()

returns node's parent node.

return
BinarySearchTreeBinarySearchTreeNode
AvlTreeAvlTreeNode

AvlTreeNode

extends BinarySearchTreeNode and adds the following methods:

.getHeight()

the height of the node in the tree. root height is 1.

return
number
.getLeftHeight()

the height of the left child. 0 if no left child.

return
number
.getRightHeight()

the height of the right child. 0 if no right child.

return
number
.calculateBalance()

returns the node's balance by subtracting right height from left height.

return
number

Build

grunt build

License

The MIT License. Full License is here

Keywords

FAQs

Package last updated on 02 Apr 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc